//
//  Created by Flutter on 2021/4/12.
//

import Foundation
import LMDB

public struct SyncStore<T: Store> {
    private let store: T

    public init(store: T) { self.store = store }
}

public extension SyncStore {
    func put<Value: DataConvertible, Key: DataConvertible>(value: Value, forKey key: Key) throws {
        try store.put(value: value, forKey: key)
    }

    func get<Value: DataConvertible, Key: DataConvertible>(key: Key) throws -> Value? {
        try store.get(key: key)
    }

    func delete<Key: DataConvertible>(key: Key) throws {
        try store.delete(key: key)
    }
}

public extension SyncStore {
    func put<StorableObject: Storable>(storableObject: StorableObject) throws {
        let key = StorableObject.key
        try put(value: storableObject, forKey: key)
    }

    func get<StorableObject: Storable>(key: String? = nil) throws -> StorableObject? {
        let key = StorableObject.key
        return try get(key: key)
    }
}

public extension SyncStore {
    func empty() throws {
        try store.empty()
    }
}
